//
//  AppDelegate.m
//  17-sandBox
//
//  Created by 王道林 on 16/8/5.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "AppDelegate.h"
#import "Book.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    // 沙盒(sandBox)
    
    //  Documents(文件文档, 用户主动数据存储)
    //  Library (资源, 一般用来存放,程序员要存储的一些数据)
    //      ↓
    //      Caches(缓存文件)
    //      preferences(用户信息,用户设置, NSUserDefaults)
    
    
    // tmp(临时目录, 下载的临时文件, 一般放这里)
    
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"wang"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    // 2 . 获取沙盒路径
    
    // 快捷获取到对应目录的c函数
    // 根目录
 //   NSLog(@"home----------%@", NSHomeDirectory());
    
    // 临时目录 tmp
    
//    NSLog(@"Temporary ------------%@", NSTemporaryDirectory());
    
    //c函数
    // 参数1:搜索哪个文件的路径
    // 参数2: 在user 作用域下搜锁
    // 参数3:  yes or no   , yes代表绝对路径(基本上都用绝对), no 代表相对的路径~
    
   NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSLog(@"%@", pathArray);
    
    
    
    // NSBundle  就是应用程序包路径
    // NSLog(@"111111111111111111111111%@", [NSBundle mainBundle]);
    
    
    
    // 1 . 简单地文件读写
    NSString *helloString = @"Hello, I/O File";
    // 一般拼接路径时. 使用
//    NSString *writePath = [[pathArray firstObject] stringByAppendingString:@"/hello.txt"];
    NSString *writePath = [[pathArray firstObject] stringByAppendingPathComponent:@"hello"];
    
    
    NSError *error = nil;
   BOOL isSuccess = [helloString writeToFile: writePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    
    if (error) {
        NSLog(@"存储失败");
    } else
    {
        NSLog(@"存储成功");
    }
    
    
    
    // 2> 读取路径对应的文字
    NSError *readError = nil;
   NSString *readString = [NSString stringWithContentsOfFile:writePath encoding:NSUTF8StringEncoding error:&readError];
    NSLog(@"%@", readString);
    
    
    // 3> 将数组写入本地文件
    
    NSArray *array = @[@"王黄", @"王黄", @"肱二头肌"];
    NSString *arrayPath = [[pathArray firstObject] stringByAppendingPathComponent:@"name.plist"];
    
   BOOL isArray = [array writeToFile:arrayPath atomically:YES];
    
    if (isArray) {
        NSLog(@"写入成功");
    } else
    {
        NSLog(@"写入失败");
    }
    
    
    //  4> 数组的 读入
    
    NSArray *arr = [NSArray arrayWithContentsOfFile:arrayPath];
    NSLog(@"%@", arr);
    
    
    // 5> 字典的写入
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"王黄", @"name", @"18", @"age", nil];
    NSString *pathDic = [[pathArray firstObject] stringByAppendingPathComponent:@"dic.txt"];
   BOOL isDic = [dic writeToFile:pathDic atomically:YES];
    
    if (isDic) {
        NSLog(@"成功");
    } else
    {
        NSLog(@"失败");
    }
    
    // 读取字典
    NSDictionary *getdic = [NSDictionary dictionaryWithContentsOfFile:pathDic];
    NSLog(@"%@", getdic);
    
    
    
    // 7>  将Data类型写入本地
    
    UIImage *image = [UIImage imageNamed:@"6.jpg"];
//    NSLog(@"%@", image);
   NSData *iamgeData = UIImageJPEGRepresentation(image, 0.1);
//        NSLog(@"%@", iamgeData);
    
    NSString *pathdate = [[pathArray firstObject] stringByAppendingPathComponent:@"iamgedate"];
   BOOL isdata = [iamgeData writeToFile:pathdate atomically:YES];
    
    if (isdata) {
        NSLog(@"成功");
    } else {
        NSLog(@"失败");
    }
    
    // 8> NSData 类型 读入
    NSData *iamgeNewData = [NSData dataWithContentsOfFile:pathdate];
    UIImage *newimage = [UIImage imageWithData:iamgeNewData];
    NSLog(@"%@", newimage);
    
    
    
    // 2. 复杂对象文件读写, 自定义类型
    //  归档/反归档      序列化/ 反序列化
    
    
    // 1> 归档, 将对象存储到本地
    Book *book = [Book new];
    book.bookName = @"iOS从入门到放弃";
    book.bookPrice = @"-5";
    book.bookAuthor = @"苍老师";
    
    NSString *bookPath = [[pathArray firstObject] stringByAppendingPathComponent:@"book.plist"];
    
   BOOL isBook = [NSKeyedArchiver archiveRootObject:book toFile:bookPath];
    
    if (isBook) {
        NSLog(@"book 成功");
    }
    else
    {
        NSLog(@"book 失败");
    }
    
    
    // 反归档
   Book *huangBook = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
    
    NSLog(@"%@", huangBook.bookName);
    
    
    
    
    // 如果对象想要实现归档和反归档
    // 1. 对象对应的类需要签订<NSCoding>协议
    // 2 . 实现协议方法
    //    1> initWithCoder   反归档用
    //    2> encodeWithCoder  归档时候用
    // 3. 归档时. 使用NSKeyedArchiver
    // 4. 反归档时. 使用NSKeyedUnarchiver
    
    
    
    
    
    
   
    
    
    
    
    
    
    
    return YES;
}






- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
